package puzzle.projecteuler.p100;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import astudy.util.AdvMath;

public class Problem049 {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		
		Integer[] tmp = AdvMath.primes(10000);
		List<Integer> plist = new ArrayList<Integer>(); 
		for (Integer p: tmp) {
			if (p > 999 && p < 10000) {
				plist.add(p);
			}
		}
		Integer[] ps = plist.toArray(new Integer[plist.size()]);
		
		for (int i = 0; i < ps.length; i ++) {
			for (int j = i+1; j < ps.length; j ++) {
				int r = 2*ps[j]-ps[i];
				if (sameDigits(ps[i], ps[j]) && sameDigits(ps[i], r) && Arrays.binarySearch(ps, r) > -1) {
					System.out.println(ps[i] + "," + ps[j] + "," + r);
				}
			}
		}
	}
	
	public static boolean sameDigits(Integer a, Integer b) {
		return reOrder(a).intValue() == reOrder(b).intValue();
	}
	
	public static Integer reOrder(Integer a) {
		List<Integer> as = new ArrayList<Integer>();
		while (a > 0 ) {
			as.add(a%10);
			a /= 10;
		}
		Integer[] tmp = as.toArray(new Integer[as.size()]);
		Arrays.sort(tmp);
		int t = 0;
		for (int d: tmp) {
			t = t*10 + d;
		}
		return t;
	}
}
